<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/images/avatar.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0px"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
      >
        <!-- 登录名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 登录密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 登录表单的验证规则对象
      loginFormRules: {
        // 验证用户名的规则数组
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        // 验证密码的规则数组
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度少于6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    // 表单预验证
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return false
        }
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (!(res.meta.status >= 200 && res.meta.status < 300)) {
          return this.$message.error('登录失败')
        }
        // console.log(Object.getPrototypeOf(this))
        this.$message.success('登录成功')
        // 1.获取token，保存到客户端
        window.sessionStorage.setItem('token', res.data.token)
        // 2.页面跳转到home, 盲猜this.$router是网址上的hash值
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: tomato;
  height: 100%;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      box-shadow: 0 0 10px gray;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: rgb(233, 213, 36);
      padding: 10px;
      display: flex;
      align-items: center;
      img {
        width: 100%;
        height: auto;
        border-radius: 50%;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 30px 0 30px;
      box-sizing: border-box;
    }
    .btn {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
